package com.kl.forum.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kl.forum.dao.*;
import com.kl.forum.entity.*;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class CircleArticleService   {
    @Autowired
    private  CircleArticleMapper circleArticleMapper;
    @Autowired
    private CircleLightMapper circleLightMapper;
    @Autowired
    private UserCoverUserMapper userCoverUserMapper;

    @Autowired
    private CircleCollectMapper circleCollectMapper;

    @Autowired
    private CirclePicMapper circlePicMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;





    public  List<CircleArticle> setPic(List<CircleArticle> list,Integer userId){
        long startTime = System.currentTimeMillis();//获取当前时间
        List<CircleArticle> listCommentNum= circleArticleMapper.countListCommentNum(list);
        List<CircleArticle> listReplyCommentNum  = circleArticleMapper.countListReplyCommentNum(list);
        List<CirclePic> circlePicList=circlePicMapper.findPicListByArticleId(list);
        List<CircleLight> circleLights=  circleLightMapper.findListCircleLight(list,userId);
        List<UserCoverUser> coverUserList= userCoverUserMapper.findListUserCoverUser(userId,list);
        List<CircleCollect> circleCollectList= circleCollectMapper.findListCircleCollect(userId,list);
            for (int i=0;i<list.size();i++){
                if (circleLights!=null){
                    for(CircleLight circleLight:circleLights){
                        if (circleLight!=null){
                            if (circleLight.getCircleId().equals(list.get(i).getId())){
                                list.get(i).setLightStatus(circleLight.getStatus());
                                break;
                            }else {
                                list.get(i).setLightStatus(0);
                            }
                        }
                    }
                }
                if (coverUserList!=null){
                    for(UserCoverUser userCoverUser:coverUserList){
                        if (userCoverUser!=null){
                            if (list.get(i).getUserId().equals(userCoverUser.getCoverUserId())){
                                list.get(i).setUserStatus(userCoverUser.getStatus());
                                break;
                            }else {
                                list.get(i).setUserStatus(0);
                            }
                        }
                    }
                }
                if (circleCollectList!=null){
                    for(CircleCollect circleCollect:circleCollectList){
                        if (circleCollect!=null){
                            if (list.get(i).getId().equals(circleCollect.getArticleId())){
                                list.get(i).setCollectStatus(circleCollect.getStatus());
                                break;
                            }else {
                                list.get(i).setCollectStatus(0);
                            }
                        }
                    }
                }
                if (circlePicList!=null) {
                    List<CirclePic> circlePics=new ArrayList<>();
                    for (int j=0;j<circlePicList.size();j++) {
                        if (circlePicList.get(j) != null) {
                            if (list.get(i).getId().equals(circlePicList.get(j).getCircleArticleId()) ) {
                                circlePics.add(circlePicList.get(j));
                            }
                        }
                        list.get(i).setPicList(circlePics);
                    }
                }
                for (CircleArticle comment:listCommentNum ){
                    if (list.get(i).getId().equals(comment.getArticleId())){
                        list.get(i).setCommentNum(comment.getCount());
                        for (CircleArticle reply: listReplyCommentNum){
                            if (comment.getArticleId().equals(reply.getArticleId())){
                                 list.get(i).setCommentNum(comment.getCount()+reply.getCount());
                            }
                        }
                    }

                }


//                //图片查询
//                List<CirclePic> circlePics= circlePicMapper.findPicByArticleId(list.get(i).getId());
//                if (circlePics!=null){
//                    list.get(i).setPicList(circlePics);
//                }
//
//
//                if (userId!=null){
//                    CircleLight circleLight= circleLightMapper.findOneCircleLight(list.get(i).getId(),userId);
//                    if(circleLight!=null){
//                        list.get(i).setLightStatus(circleLight.getStatus());
//                    }else {
//                        list.get(i).setLightStatus(0);
//                    }
//
//                    UserCoverUser userCoverUser= userCoverUserMapper.findOneUserCoverUser(userId,list.get(i).getUserId());
//                    if (userCoverUser!=null){
//                        list.get(i).setUserStatus(userCoverUser.getStatus());
//                    }else {
//                        list.get(i).setUserStatus(0);
//                    }
//
//
//                    CircleCollect circleCollect=  circleCollectMapper.findOneCircleCollect(userId,list.get(i).getId());
//                    if (circleCollect!=null){
//                        list.get(i).setCollectStatus( circleCollect.getStatus());
//                    }else {
//                        list.get(i).setCollectStatus(0);
//                    }
//                }
//
//                int commentNum= circleArticleMapper.countCommentNum(list.get(i).getId());
//                int replyCommentNum  = circleArticleMapper.countReplyCommentNum(list.get(i).getId());
//                list.get(i).setCommentNum(commentNum+replyCommentNum);

                Object articleViewNumCount=stringRedisTemplate.opsForHash().get("circleArticleViewNumCount",list.get(i).getId().toString());
                if (articleViewNumCount==null){
                    stringRedisTemplate.opsForHash().put("circleArticleViewNumCount",list.get(i).getId().toString(),"0");
                    list.get(i).setViewNum(0);
                }else {
                    list.get(i).setViewNum(Integer.parseInt(articleViewNumCount.toString()));
                }
                Object articleLightNumCount=stringRedisTemplate.opsForHash().get("circleArticleLightNumCount",list.get(i).getId().toString());
               if (articleLightNumCount==null){
                   stringRedisTemplate.opsForHash().put("circleArticleLightNumCount",list.get(i).getId().toString(),"0");
                   list.get(i).setLightNum(0);
               }else {
                   list.get(i).setLightNum(Integer.parseInt(articleLightNumCount.toString()));
               }

        }

        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间Testnew："+(endTime-startTime)+"ms");
        return list;
    }

    public PageInfo findEssenceCircle(Integer userId,Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        List<CircleArticle> list=circleArticleMapper.findEssenceCircle(map);

        if (list.size()!=0) {
            list = setPic(list, userId);
        }
        PageInfo<CircleArticle> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    public  int insert(CircleArticle circleArticle){
       return circleArticleMapper.insertSelective(circleArticle);
    }


    public int insertCircleArticle(Integer userId, Integer circleId, String content, String video,String videoScreenshots ) {
         CircleArticle circleArticle=new CircleArticle();
         circleArticle.setUserId(userId);
         circleArticle.setCircleId(circleId);
         circleArticle.setContent(content);
         circleArticle.setVideo(video);
         circleArticle.setIsDel(1);
         circleArticle.setVideoScreenshots(videoScreenshots);
         circleArticle.setCreateTime(new Date());
         circleArticle.setReplyTime(new Date());

        circleArticleMapper.insertSelective(circleArticle);
         return circleArticle.getId();
    }

    public  int delCircleArticle(Integer circleArticleId){
        CircleArticle circleArticle=new CircleArticle();
        circleArticle.setIsDel(0);
        circleArticle.setId(circleArticleId);
        return circleArticleMapper.updateByPrimaryKeySelective(circleArticle);
    }

    public  CircleArticle findOneCircleArt(Integer id){
       return circleArticleMapper.selectByPrimaryKey(id);
    }
    public int update(CircleArticle circleArticle){
        return circleArticleMapper.updateByPrimaryKeySelective(circleArticle);
    }


    public List<Map> findCircleArticleTop(Integer circleId) {
        Map map=new HashMap();
        map.put("circleId",circleId);
      return   circleArticleMapper.findCircleArticleTop(map);
    }

    public PageInfo findCircleArticleBySort(Integer userId,Integer circleId,Integer pageNum,Integer pageSize,String sort) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        map.put("circleId",circleId);
        map.put("sort",sort);
        long startTime = System.currentTimeMillis();//获取当前时间
        List<CircleArticle> list=circleArticleMapper.findCircleArticleBySort(map);

        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间Test2："+(endTime-startTime)+"ms");
        if (list.size()!=0) {
            list = setPic(list, userId);
        }
        PageInfo<CircleArticle> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }


    public PageInfo searchCircleArticle(String keyWord, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("keyWord",keyWord);
        List<Map> list=circleArticleMapper.searchCircleArticle(map);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public PageInfo findMyLight( Integer userId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        List<Map> list=circleArticleMapper.findMyLight(map);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public PageInfo findMyComment(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        List<Map> list=circleArticleMapper.findMyComment(map);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public CircleArticle findCircleArticleDetail(Integer userId, Integer id) {
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        map.put("id",id);
        CircleArticle circleArticle= circleArticleMapper.findCircleArticleDetail(map);
        if (!stringRedisTemplate.opsForHash().hasKey("circleArticleViewNumCount",String.valueOf(circleArticle.getId()))){
            stringRedisTemplate.opsForHash().put("circleArticleViewNumCount",String.valueOf(circleArticle.getId()),"1");
        }else {
            stringRedisTemplate.opsForHash().increment("circleArticleViewNumCount",String.valueOf(circleArticle.getId()),1);
        }
        Object articleViewNumCount=stringRedisTemplate.opsForHash().get("circleArticleViewNumCount",String.valueOf(circleArticle.getId()));
        circleArticle.setViewNum(Integer.parseInt(String.valueOf(articleViewNumCount)));
        if (!stringRedisTemplate.opsForHash().hasKey("circleArticleLightNumCount",String.valueOf(circleArticle.getId()))){
            stringRedisTemplate.opsForHash().put("circleArticleLightNumCount",String.valueOf(circleArticle.getId()),"0");
        }
        Object articleLightNumCount=stringRedisTemplate.opsForHash().get("circleArticleLightNumCount",String.valueOf(circleArticle.getId()));
        circleArticle.setLightNum(Integer.parseInt(String.valueOf(articleLightNumCount)));
        int commentNum= circleArticleMapper.countCommentNum(id);
        int replyCommentNum  = circleArticleMapper.countReplyCommentNum(id);
        circleArticle.setCommentNum(commentNum+replyCommentNum);
        if (userId!=null){
//            CircleArticle circleArticle= circleArticleMapper.findCircleArticleDetail(map);
            CircleLight circleLight= circleLightMapper.findOneCircleLight(circleArticle.getId(),userId);
            UserCoverUser userCoverUser= userCoverUserMapper.findOneUserCoverUser(userId,circleArticle.getUserId());
            CircleCollect circleCollect=  circleCollectMapper.findOneCircleCollect(userId,circleArticle.getId());
            if (circleLight!=null){
                circleArticle.setLightStatus(circleLight.getStatus());
            }else {
                circleArticle.setLightStatus(0);
            }

            if (userCoverUser!=null){
                circleArticle.setUserStatus(userCoverUser.getStatus());
            }else {
                circleArticle.setUserStatus(0);
            }

            if (circleCollect!=null){
                circleArticle.setCollectStatus(circleCollect.getStatus());
            }else {
                circleArticle.setCollectStatus(0);
            }
            List<CirclePic> circlePics= circlePicMapper.findPicByArticleId(circleArticle.getId());
            if (circlePics!=null){
                circleArticle.setPicList(circlePics);
            }

            return circleArticle;
        }else {
//            CircleArticle circleArticle= circleArticleMapper.findCircleArticleDetail(map);
            List<CirclePic> circlePics= circlePicMapper.findPicByArticleId(circleArticle.getId());
            if (circlePics!=null){
                circleArticle.setPicList(circlePics);
            }

            return circleArticle;
        }
    }


    public PageInfo findMyFollowUserCircle(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        List<CircleArticle> list=circleArticleMapper.findMyFollowUserCircle(map);
        if (list.size()!=0) {
            list = setPic(list, userId);
        }
        PageInfo<CircleArticle> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


}
